# How Does Improvement in Commuting Affect Employees? Evidence from a Natural Experiment
# Version: 20211014

## Yao Lu (luyao@sem.tsinghua.edu.cn),        Tsinghua University
## Xinzheng Shi (shixzh@sem.tsinghua.edu.cn), Tsinghua University
## Jagadeesh Sivadasan (jagadees@umich.edu),  University of Michigan
## Zhufeng Xu (zhufeng@zhufengxu.com),        Central University of Finance and Economics

if (!exists("AllScriptFlag", 1)) {
  source("./Scripts/1Header.R")
}

coef_levels <- c("2013-01", "2013-02", "2013-03", "2013-04", "2013-05", "2013-06", 
                 "2013-07", "2013-08", "2013-09", "2013-10", "2013-11", "2013-12",
                 "2014-01", "2014-02", "2014-03", "2014-04", "2014-05", "2014-06", 
                 "2014-07", "2014-08", "2014-09", "2014-10", "2014-11", "2014-12",
                 "2015-01", "2015-02", "2015-03", "2015-04", "2015-05", "2015-06", 
                 "2015-07", "2015-08", "2015-09", "2015-10", "2015-11", "2015-12",
                 "2016-01", "2016-02", "2016-03", "2016-04", "2016-05", "2016-06", 
                 "2016-07", "2016-08", "2016-09", "2016-10", "2016-11")

SurvivalFunction <- read_dta("Data/SurvivalFunction.dta")
SurvivalFunction <- mutate(SurvivalFunction, NearSubway = factor(NearSubway, c(0, 1), c("Unaffected", "Affected")))
fit_SurvivalFunction <- survfit(Surv(time, status) ~ NearSubway, data = SurvivalFunction)

autoplot(fit_SurvivalFunction, 
         surv.size = c(rep(1.0, 47), rep(2.0, 45)),
         conf.int = FALSE, censor.size = 4) + 
  ylab("Survival Rate") + xlab("Exit Date") +
  scale_x_continuous(breaks= seq(1, 47, 1), labels= coef_levels) +
  scale_colour_manual(name  ="NearSubway", breaks=c("Unaffected", "Affected"), labels=c("Unaffected", "Affected"),
                      values = c("darkgrey", "black")) +
  geom_vline(aes(xintercept = 24.5), colour = "black", linetype = "dashed", size = 1.4) + 
  theme_light() +
  guides(colour = guide_legend(override.aes = list(size = c(1.0, 2.0)))) +
  theme(legend.position = c(0.85, 0.85)) +
  theme(axis.text.x  = element_text(angle = 30, hjust = 1)) +
  theme(text=element_text(family = "Times New Roman", size = 12, lineheight = 2))
ggsave("Output/Figure4.png", width = 11, height = 4.25, unit = "in")
ggsave("Output/Figure4.pdf", width = 11, height = 4.25, unit = "in", device = cairo_pdf)

rm(SurvivalFunction)
